// components/searchBar/searchBar.js
let app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    doctorsTitle: {
      type: String,
      value: '部位病种筛选'
    },
    timeTitle: {
      type: String,
      value: '时间筛选'
    },
    doctorsPlaceholder: {
      type: String,
      value: '请选择部位病种'
    }
  },
  lifetimes: {
    ready() {
      const _this = this
      app.on('search', this, function (value) {
        _this.setData({
          value: value
        })
      })
      wx.request({
        url: app.globalData.serverUrlgdb + '/applet/treat/list',
        method: 'GET',
        header: {
          'token': wx.getStorageSync('token')
        },
        success:(res=>{
         let resData = res.data
         if(resData.code==200){
           console.log(resData.data.treatLists);
           resData.data.treatLists.map(item=>{
             item.label = item.treatmentName
             item.value = item.treatmentId
           })
           _this.setData({
             options:resData.data.treatLists
           })
         }
        })
      })
    },

  },
  /**
   * 组件的初始数据
   */
  data: {
    showFilter: false,
    showFilters: false,
    startDate: '开始日期',
    endDate: '结束日期',
    startDate1: "",
    endDate1: "",
    startDate2: "",
    endDate2: "",
    doctors: "",
    medicalActive: false,
    treatment: '',
    treatmentActive: false,
    options: [
    ],
    bottom: '',
    treatmentBottom: '',
    doctorsId:''
  },
  /**
   * 组件的方法列表
   */

  methods: {
    bindtapFilter() {
      this.setData({
        showFilter: !this.data.showFilter,
        showFilters: true
      })
      let _this = this
      wx.createSelectorQuery().in(this).select('#medical').boundingClientRect(function (rect) {
        _this.setData({
          bottom: rect?.bottom
        })
      }).exec()
      wx.createSelectorQuery().in(this).select('#treatment').boundingClientRect(function (rect) {
        _this.setData({
          treatmentBottom: rect?.bottom
        })
      }).exec()
    },
    bindtapMedical() {
      this.setData({
        treatmentActive: false,
        medicalActive: !this.data.medicalActive
      })
    },
    bindChangeMedical(e) {
      this.setData({
        doctors: e.detail.label,
        doctorsId: e.detail.value,
        medicalActive: false
      })
    },
    bindtapTreatment() {
      this.setData({
        medicalActive: false,
        treatmentActive: !this.data.treatmentActive
      })
    },
    bindChangeTreatment(e) {
      this.setData({
        treatment: e.detail.value,
        treatmentActive: false
      })
    },
    /**
     * 用户点击重置
     */
    bindtapReset() {
      this.setData({
        doctors: '',
        doctorsId:'',
        startDate: '开始日期',
        endDate: '结束日期',
        startDate1: '',
        endDate1: '',
      })
      this.triggerEvent('change', {
        doctors: '',
        startDate: '',
        endDate: '',
        doctorsId:'',
      })
    },
    /**
     * 用户点击确定
     */
    bindStartDateChange(e) {
      this.setData({
        startDate2: e.detail.value,
        startDate: e.detail.value
      })
    },
    bindEndDateChange(e) {
      this.setData({
        endDate1: e.detail.value,
        endDate: e.detail.value
      })
    },
    bindtapConfirm() {
      const {
        doctors,
        startDate,
        endDate,
        doctorsId
      } = this.data
      this.triggerEvent('change', {
        doctors: doctors,
        startDate: startDate,
        endDate: endDate,
        doctorsId:doctorsId
      })
      
      this.setData({
        showFilter: false,
        showFilters:doctorsId==''?false:true
      })
    }
  }
})